from django.shortcuts import render
from django.http import *
from Register.models import *
from User_info.views import send_massage


# Create your views here.


def register(request):
    # get请求说明用户初次请求页面
    if request.method == "GET":
        return render(request, "User.html")

    # post 请明用户提交了表单
    if request.method == "POST":
        username = request.POST.get("username")
        password = request.POST.get("password")
        email = request.POST.get("email")
        try:
            email_obj = Email()
            email_obj.email_addr = email
            email_obj.save()
            s1 = Student()
            s1.name = username
            s1.password = password
            s1.email = email_obj
            s1.save()
            # 发送用户信息通知
            send_massage(username, email)
            return JsonResponse({
                "status_num": 0,
                "url": "/login/"
            })
        except Exception as e:
            print("报错了!")
            print(e)
            return JsonResponse({"status_num": 1, })
